const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');


  let users = await db.collection('users')
    .find(

    )
    .toArray();


  for (let i = 0; i < users.length; i++) {
    let user = users[i];

    let followings = user.followings;
    let fans = user.fans;
    if (followings == null || followings == undefined) {
      followings = [];
    }
    if (fans == null || fans == undefined) {
      fans = [];
    }

    try {
      let result = await db.collection('users').updateOne({_id: user._id}, {
        $set: {
          followings: followings,
          fans: fans,
        },
      });
      console.log(result.result);
    } catch (e) {
      console.error(e.message);
    }
  }

  client.close();

}

main();
